using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Xfinity.ParticleSystems.Emitters
{
    /// <summary>
    /// A simple point emitter.
    /// </summary>
    public class PointEmitter : ParticleEmitter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PointEmitter"/> class.
        /// </summary>
        /// <param name="position">The position.</param>
        public PointEmitter(Vector3 position)
            : base(position)
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PointEmitter"/> class.
        /// </summary>
        public PointEmitter()
            : this(Vector3.Zero)
        {

        }

        private static Random r = new Random();
        protected override float GenerateDeathAge()
        {
            float time = ((float)r.NextDouble()) * DeathSpan;
            time += MinimumDeathAge;
            return time;

        }

        /// <summary>
        /// Generates the initial color.
        /// </summary>
        /// <returns></returns>
        protected override Microsoft.Xna.Framework.Graphics.Color GenerateInitialColor()
        {

            float r1 = (float)r.NextDouble();
            float r2 = (float)r.NextDouble();
            float r3 = (float)r.NextDouble();
            float r4 = (float)r.NextDouble();

            Vector4 interpolant = new Vector4(r1, r2, r3, r4);

            interpolant *= ColorSpan.ToVector4();
            interpolant += MinimumColorValue.ToVector4();
            return new Color(interpolant);

        }

        protected override float GenerateInitialSize()
        {
            return ((float)r.NextDouble() * SizeSpan) + MinimumSize;
        }

        protected override Microsoft.Xna.Framework.Vector3 GenerateInitialAcceleration()
        {
            float doubleAccelerationSpan = 2 * AccelerationSpan;
            float r1 = ((float)r.NextDouble() - .5f) * doubleAccelerationSpan;
            float r2 = ((float)r.NextDouble() - .5f) * doubleAccelerationSpan;
            float r3 = ((float)r.NextDouble() - .5f) * doubleAccelerationSpan;

            return new Vector3(r1, r2, r3);
        }

        protected override Microsoft.Xna.Framework.Vector3 GenerateInitialVelocity()
        {
            float doubleSpeedSpan = 2 * SpeedSpan;
            float r1 = ((float)r.NextDouble() - .5f) * doubleSpeedSpan;
            float r2 = ((float)r.NextDouble() - .5f) * doubleSpeedSpan;
            float r3 = ((float)r.NextDouble() - .5f) * doubleSpeedSpan;

            return new Vector3(r1, r2, r3);

        }

        protected override Microsoft.Xna.Framework.Vector3 GenerateInitialPosition()
        {
            return this.CentrePosition;
        }
    }
}
